package org.newsexample.ws;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.newsexample.www.news.News;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class NewsDAO 
{
	public News[] getAllNews() 
	{
		News[] news = null;
		try {
	
		String URLString = "http://blogs.sjsu.edu/today/feed/";
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		news = new News[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			
			System.out.println("title"+getElementValue(element,"title"));
			
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String category = getElementValue(element,"category");
			String pubdate = getElementValue(element,"pubDate");
			String description =  getElementValue(element,"description");
			String author =  getElementValue(element,"dc:creator");
			
			news[i] = new News();
			news[i].setNews_id(i);
		    news[i].setNews_title(title);
		    news[i].setNews_link(link);
		    news[i].setNews_category(category);
		    news[i].setNews_publication_date(pubdate);
		    news[i].setNews_description(description);
		    news[i].setNews_author(author);		   
		}//for
		}//try
		catch(Exception ex) 
		{
			//throw sevice unavailable exception
			ex.printStackTrace();
		}
		return news;
		}
	
	public News[] getNewsByKeyword(String keyword) 
	{
		News[] news = null;
		try {
	
		String URLString = "http://blogs.sjsu.edu/today/feed/?s=" + keyword;
		//String URLString = "http://blogs.sjsu.edu/today/feed/";
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		news = new News[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			
			System.out.println("title"+getElementValue(element,"title"));
			
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String category = getElementValue(element,"category");
			String pubdate = getElementValue(element,"pubDate");
			String description =  getElementValue(element,"description");
			String author =  getElementValue(element,"dc:creator");
			
			news[i] = new News();
			news[i].setNews_id(i);
		    news[i].setNews_title(title);
		    news[i].setNews_link(link);
		    news[i].setNews_category(category);
		    news[i].setNews_publication_date(pubdate);
		    news[i].setNews_description(description);
		    news[i].setNews_author(author);		   
		}//for
		}//try
		catch(Exception ex) 
		{
			//throw sevice unavailable exception
			ex.printStackTrace();
		}
		return news;
		}
	
	public News[] getNewsByCategory(String category) 
	{
		News[] news = null;
		try {
	
		String URLString = "http://blogs.sjsu.edu/today/feed/?s=" + category;
		//String URLString = "http://blogs.sjsu.edu/today/feed/";
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		news = new News[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			
			System.out.println("title"+getElementValue(element,"title"));
			
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String category1 = getElementValue(element,"category");
			String pubdate = getElementValue(element,"pubDate");
			String description =  getElementValue(element,"description");
			String author =  getElementValue(element,"dc:creator");
			
			news[i] = new News();
			news[i].setNews_id(i);
		    news[i].setNews_title(title);
		    news[i].setNews_link(link);
		    news[i].setNews_category(category1);
		    news[i].setNews_publication_date(pubdate);
		    news[i].setNews_description(description);
		    news[i].setNews_author(author);		   
		}//for
		}//try
		catch(Exception ex) 
		{
			//throw sevice unavailable exception
			ex.printStackTrace();
		}
		return news;
		}
	
	

		protected float getFloat(String value) {
		if(value != null && !value.equals("")) {
		return Float.parseFloat(value);
		}
		return 0;
		}

		private String getCharacterDataFromElement(Element e) {
			try {
			Node child = e.getFirstChild();
			if(child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
			}
			}
			catch(Exception ex) {

			}
			return "";
			} //private String getCharacterDataFromElement

		
		protected String getElementValue(Element parent,String label) {
		return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
		}	
}
